public class SoundMessage extends Message {

    public SoundMessage(TIMMessage message){
        this.message = message;
    }

    public SoundMessage(long duration,String filePath){
        message = new TIMMessage();
        TIMSoundElem elem = new TIMSoundElem();
        elem.setPath(filePath);
        elem.setDuration(duration);
        message.addElement(elem);
    }

    @Override
    public void showMessage(ChatAdapter.ViewHolder viewHolder, Context context) {
        if (checkRevoke(viewHolder)) return;
        String munber=String.valueOf(((TIMSoundElem) message.getElement(0)).getDuration()) + "\"";
        int aaa=Integer.valueOf(munber.replaceAll("\"",""));

        LinearLayout linearLayout = new LinearLayout(MyApplication.getContext());

        linearLayout.setBackgroundResource(message.isSelf()?R.drawable.chat_r:R.drawable.chat_l);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(time2lengh(aaa), ViewGroup.LayoutParams.WRAP_CONTENT));
            
            ImageView voiceIcon = new ImageView(MyApplication.getContext());
            final AnimationDrawable frameAnimatio = (AnimationDrawable) voiceIcon.getBackground();
            voiceIcon.setBackgroundResource(message.isSelf()?R.drawable.right_voice: R.drawable.left_voice);
            voiceIcon.setLayoutParams(new LinearLayout.LayoutParams(PxUtil.souH, PxUtil.souW));

        linearLayout.addView(voiceIcon);

        clearView(viewHolder,true,context);
        if(message.isSelf()){
            viewHolder.rv.setText(munber);
        }else {
            viewHolder.lv.setText(munber);
        }
        getBubbleView(viewHolder,true,context).addView(linearLayout);
        getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VoiceMessage.this.playAudio(frameAnimatio);
            }
        });
        showStatus(viewHolder);
    }

    @Override
    public String getSummary() {
        return MyApplication.getContext().getString(R.string.summary_voice);
    }

    @Override
    public void save() {
    }

    private void playAudio(final AnimationDrawable frameAnimatio) {
        TIMSoundElem elem = (TIMSoundElem) message.getElement(0);
        final File tempAudio = FileUtil.getTempFile(FileUtil.FileType.AUDIO);
        elem.getSoundToFile(tempAudio.getAbsolutePath(), new TIMCallBack() {
            @Override
            public void onError(int i, String s) {
            }
            @Override
            public void onSuccess() {
                try {
                    FileInputStream fis = new FileInputStream(tempAudio);
                    MediaUtil.getInstance().play(fis);
                    frameAnimatio.start();
                    MediaUtil.getInstance().setEventListener(new MediaUtil.EventListener() {
                        @Override
                        public void onStop() {
                            frameAnimatio.stop();
                            frameAnimatio.selectDrawable(0);
                        }
                    });
                }catch (Exception e) {
                }
            }
        });
    }

    private int time2lengh(int time){
       int a=80;
       if(time>0&&time<=10){
           a=a+(7*time);
           return MyApplication.getInstance().dp2px(a);
       }else {
           a=a+time+60;
           return MyApplication.getInstance().dp2px(a);
       }
    }
}
